//
//  ProfileTableViewController.m
//  GitBucket
//
//  Created by 3024 on 15/11/3.
//  Copyright © 2015年 ios25. All rights reserved.
//

#import "ProfileTableViewController.h"
#import "JsonData.h"
#import "UIImage+UIImageExtras.h"
#define NavigationBarHight 64.0f
#define ImageHight 200.0f
@interface ProfileTableViewController ()<UIScrollViewDelegate,UITableViewDelegate>
{
    JsonData *_jsonData;
    UIImageView *_zoomImageView;
}
@property (weak, nonatomic) IBOutlet UILabel *blogLabel;

@property (weak, nonatomic) IBOutlet UILabel *followersLabel;
@property (weak, nonatomic) IBOutlet UILabel *repositoriesLabel;
@property (weak, nonatomic) IBOutlet UILabel *followingLabel;
@property (weak, nonatomic) IBOutlet UILabel *groupLabel;
@property (weak, nonatomic) IBOutlet UILabel *locationLabel;
@property (weak, nonatomic) IBOutlet UILabel *emailLabel;
@property (weak, nonatomic) IBOutlet UILabel *connectLabel;

@end

@implementation ProfileTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _jsonData = [JsonData allowInitDefault];
    
    
    
//self.edgesForExtendedLayout = UIRectEdgeNone;
//    _zoomImageView = [[UIImageView alloc]initWithImage:[_jsonData.avatar imageByScalingToSize:CGSizeMake(640, 400)]]	;
//
//    self.tableView.contentInset = UIEdgeInsetsMake(ImageHight, 0, 0, 0);
//
//    _zoomImageView.frame = CGRectMake(0, -ImageHight, self.view.frame.size.width, ImageHight);
//    
//
//    _zoomImageView.contentMode = UIViewContentModeScaleAspectFill;//重点（不设置那将只会被纵向拉伸）
//    
//    [self.tableView addSubview:_zoomImageView];
//
    self.tableView.contentInset = UIEdgeInsetsMake(ImageHight, 0, 0, 0);
    _zoomImageView = [[UIImageView alloc]initWithImage:_jsonData.avatar]	;
    _zoomImageView.frame = CGRectMake(0, -ImageHight, self.view.frame.size.width, ImageHight);
    
    //contentMode = UIViewContentModeScaleAspectFill时，高度改变宽度也跟着改变
    _zoomImageView.contentMode = UIViewContentModeScaleAspectFill;//重点（不设置那将只会被纵向拉伸）
    
    [self.tableView addSubview:_zoomImageView];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    UIImageView *avatar = [[UIImageView alloc]initWithImage:_jsonData.avatar];
    avatar.frame = CGRectMake(0, 0, 100, 100);
    avatar.center = _zoomImageView.center;
    avatar.layer.masksToBounds = YES;
    avatar.layer.cornerRadius = 50;
    avatar.alpha = 0.8;
    [self.tableView addSubview:avatar];
    
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(30, -30, self.view.frame.size.width, 30)];
    name.text = _jsonData.name;
    name.textColor = [UIColor blueColor];
    [self.tableView addSubview:name];
    //初始化数据
    
//    _userNameLabel.text = _jsonData.login;
//    _hiddenImageView.image = _jsonData.avatar;
    _followersLabel.text = [NSString stringWithFormat:@"%lu",_jsonData.followers];
    _repositoriesLabel.text = [NSString stringWithFormat:@"%lu",_jsonData.public_repos];
    _followingLabel.text = [NSString stringWithFormat:@"%lu",_jsonData.following];
    _groupLabel.text = _jsonData.company;
    _locationLabel.text = _jsonData.location;
    _emailLabel.text = _jsonData.email;
    _blogLabel.text = _jsonData.blog;
    
    self.tableView.delegate = self;
    

    
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    //显示底部
    self.tabBarController.tabBar.hidden = NO;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    return 20;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat y = scrollView.contentOffset.y -NavigationBarHight;
    NSLog(@"%f",y);
    if (y < -ImageHight-NavigationBarHight) {
        CGRect frame = _zoomImageView.frame;
        frame.origin.y = y;
        frame.size.height = -y;
        _zoomImageView.frame = frame;
        
    }
    
    
    
//    //比如查看移动的偏移量
//    CGPoint offset = scrollView.contentOffset;
//    
//    NSLog(@"did scroll %lf,%lf",offset.x,offset.y);
//    
//    if (offset.y < -64) {
//        
//        //1.获取捏合缩放的比例
//        CGFloat scale = offset.y/64;
//        NSLog(@"scale:%f",scale);
//        
//        //2.根据图片调整图片大小
//        _imageView.transform = CGAffineTransformMakeScale(-scale, -scale);
//        
//        //改变隐藏图片/label的透明度
//        _hiddenImageView.alpha = scale+2;
//        _userNameLabel.alpha = scale+2;
//    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//
//    return 3;
//}

//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//
//    return 0;
//}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//表视图的代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.row == 2) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hzlzh.com"]];
    }
    if (indexPath.section == 1 && indexPath.row == 3) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_blogLabel.text]];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
